// pages/my/edit/index.js
import {
  getInfoApi,
  putInfoApi
} from '../../../https/user'
import {
  BASE_URL
} from '../../../constant/index'
import {uploadFileAsync} from '../../../utlis/utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    sexList: ["保密", "女", "男"],
    heightList: [],
    weightList: [],
    horoscopeList: ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'],
    BASE_URL
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()
    this.getUserInfo()

  },
  // 初始化数据
  init() {
    let heightList = []
    let weightList = []
    for (let i = 35; i < 220; i++) {
      if (i > 130) {
        heightList.push(i + 'cm')
      }
      weightList.push(i + 'kg')
    }
    this.setData({
      heightList,
      weightList
    })
  },
  // 选项框
  async bindPickerChange(e) {
    const type = e.currentTarget.dataset.type
    const key = e.currentTarget.dataset.key

    let value = e.detail.value

    // if (type == 'sex') {
    //   value = Number(value) - 1
    // }
    if (type == 'city') {
      value = value.join('-')
    }

    if (key) {
      value = this.data[key][value]
      console.log(value)
    }

    console.log(type, value)
    wx.showLoading()
    const putRes = await putInfoApi({
      [type]: value
    })

    console.log(putRes)
    this.getUserInfo()
    wx.hideLoading()
  },
  // 获取用户信息
  async getUserInfo() {
    const getRes = await getInfoApi()

    if (getRes.code == 200) {
      console.log(getRes.data)
      getRes.data.cityList = getRes.data.city?.split('-')
      getRes.data.weightIndex = this.data.weightList.indexOf(getRes.data.weight)
      getRes.data.heightIndex = this.data.heightList.indexOf(getRes.data.height)
      getRes.data.horoscopeIndex = this.data.horoscopeList.indexOf(getRes.data.horoscope)
      getRes.data.sexIndex = this.data.sexList.indexOf(getRes.data.sex)

      this.setData({
        info: getRes.data
      })
    }
  },
  // 编辑昵称
  async bindEditNikeName() {
    const nikeName = this.data.info.nikeName
    const {
      content,
      confirm
    } = await wx.showModal({
      title: '请输入您的昵称',
      placeholderText: nikeName,
      editable: true,
    })
    if (!confirm) return
    if (content.length < 3 || content.length > 12) {
      wx.showToast({
        title: '昵称长度为3-12位字符',
        icon: 'none'
      })
      return
    }
    wx.showLoading()
    await putInfoApi({
      nikeName: content
    })
    this.getUserInfo()
    wx.hideLoading()
  },
  // 编辑简介
  async bindEditDesc() {
    const desc = this.data.info.desc
    const {
      content,
      confirm
    } = await wx.showModal({
      title: '请输入您的简介',
      placeholderText: desc,
      editable: true,
    })
    if (!confirm) return
    if (content.length > 32) {
      wx.showToast({
        title: '简介最长为32位字符',
        icon: 'none'
      })
      return
    }
    wx.showLoading()
    await putInfoApi({
      desc: content
    })
    this.getUserInfo()
    wx.hideLoading()
  },
  // 更换头像
  async bindEditAvatar(e) {
    const {
      tempFiles
    } = await wx.chooseMedia({
      count: 1,
      mediaType: ['image']
    })
    wx.showLoading()
    const url = await uploadFileAsync(tempFiles[0].tempFilePath)
    console.log(url)
    await putInfoApi({
      avatar: url
    })
    this.getUserInfo()
    wx.hideLoading()

  },
  // 更换背景图
  async bindBackground(e) {
    const {
      tempFiles
    } = await wx.chooseMedia({
      count: 1,
      mediaType: ['image']
    })
    wx.showLoading()
    const url = await uploadFileAsync(tempFiles[0].tempFilePath)
    console.log(url)
    await putInfoApi({
      background: url
    })
    this.getUserInfo()
    wx.hideLoading()

  },
  // // 文件上传
  // async uploadFile(filePath) {
  //   return new Promise((resolv, reject) => {
  //     wx.uploadFile({
  //       filePath,
  //       name: 'files',
  //       url: this.data.BASE_URL + '/api/upload',
  //       async success(res) {
  //         const result = JSON.parse(res.data)
  //         console.log(result)
  //         if (result.code == 200) {
  //           resolv(result.data[0])
  //         } else {
  //           reject()
  //         }
  //       },
  //       fail(err) {
  //         console.log(err)
  //         reject()
  //       }
  //     })
  //   })
  // }

})